from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles

from utils.exception import fastapi_exception_handler
from utils.global_variable import emit_global_variable
from utils.helper import load_yaml
from utils.paths import SYSTEM_CONFIG_PATH
from api.router import api_router
from web.router import web_router

system_config = load_yaml(SYSTEM_CONFIG_PATH)

app = FastAPI()

# 异常拦截处理
fastapi_exception_handler(app)

# 注入全局变量
emit_global_variable(app)

# CORS限制
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
    expose_headers=['*'],
)

# 指定静态文件目录
app.mount('/static', StaticFiles(directory='web/static'), name='static')

# 导入路由
app.include_router(api_router)
app.include_router(web_router)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host=system_config['HOST'], port=system_config['PORT'])
